package com.xueyi.aixihu.mapper;

import com.xueyi.aixihu.entity.vo.FindRuleByRoleVo;
import com.xueyi.aixihu.entity.vo.FindRuleFatherVo;
import com.xueyi.aixihu.entity.vo.TreeOut;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author lvyi
 * @date : 2022/8/19
 * @Description : 权限表的mapper接口
 */
public interface LocalDtAuthRuleMapper {


    /**
     * 根据角色id查询其权限id
     *
     * @return
     */
    @Select(" SELECT rules ruleId FROM dt_auth_group WHERE id = #{id} AND status = 1 ")
    FindRuleByRoleVo findRuleByRole(Integer id);

    /**
     * 查询出所有的顶级父类权限名称和id
     *
     * @return
     */
    @Select(" SELECT " +
            " ar.title ruleTitle, " +
            " ar.id ruleId " +
            " FROM " +
            " dt_auth_rule ar " +
            " LEFT JOIN dt_auth_rule ad ON ar.pid = ad.id  " +
            " WHERE " +
            " ar.pid = 0 " +
            " AND ar.status = 1 ")
    List<FindRuleFatherVo> findRuleFather();

    /**
     * 查询该父类id下面的所有子类id
     *
     * @param id
     * @return
     */
    @Select(" SELECT title ruleTitle,id ruleId FROM dt_auth_rule WHERE pid = #{id} AND status = 1 ")
    List<TreeOut> findRuleByFather(Integer id);

    /**
     * 根据角色id关联权限
     *
     * @param id     角色id
     * @param ruleId 权限id
     */
    @Update(" UPDATE dt_auth_group SET rules = #{ruleId} WHERE id = #{id} ")
    void updateRulesByRole(Integer id, String ruleId);

}
